package com.kaili.basic.controller.web.v1;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.BaseMenuAddDto;
import com.kaili.basic.pojo.dto.BaseRoleAddMenuDto;
import com.kaili.basic.pojo.dto.BaseRoleUpdateDto;
import com.kaili.basic.service.BaseDepartmentService;
import com.kaili.basic.service.BaseMenuRoleService;
import com.kaili.basic.service.BaseMenuService;
import com.kaili.basic.service.BaseRoleService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.dto.BaseDepartmentqueryDto;
import com.kaili.common.entity.BaseDepartment;
import com.kaili.common.entity.BaseDepartmentUpdateVO;
import com.kaili.common.entity.BaseMenu;
import com.kaili.common.entity.BaseRole;
import com.kaili.common.exception.RestException;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.ExcelUtil;
import com.kaili.common.util.LoginDetailUtils;
import com.kaili.common.util.UUIDUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@ApiVersion(1)
@RequestMapping("/{api_version}")
@Api(tags = "菜单角色相关设置", description = "菜单角色相关设置API")
public class BaseBasicController {
    @Autowired
    BaseRoleService baseRoleService;
    @Autowired
    BaseMenuService baseMenuService;
    @Autowired
    BaseMenuRoleService baseMenuBaseRoleService;
    @Autowired
    BaseDepartmentService baseDepartmentService;

    Logger log = LoggerFactory.getLogger(BaseBasicController.class);

    @DeleteMapping(value = "baseRole/{rid}")
    @ApiOperation(value = "删除角色", notes = "根据角色Id删除角色")
    public RestStatus deleteBaseRole(@PathVariable Long rid) {
        baseRoleService.deleteBaseRoleById(rid);
        return CommonStatus.DELETE_OK;
    }

    @PostMapping(value = "baseRole")
    @ApiOperation(value = "添加角色", notes = "添加角色")
    public Object addNewBaseRole(@Validated @RequestBody BaseMenuAddDto baseMenuAddDto) {
        BaseRole baseRole = new BaseRole();
        baseRole.setName(baseMenuAddDto.getName());
        baseRole.setNameZh(baseMenuAddDto.getName());
        baseRole.setRemark(baseMenuAddDto.getRemark());
//        BaseRole baseRole1 = new BaseRole();
        BaseRole baseRole2 = new BaseRole();
//        baseRole1.setNameZh(baseRole.getNameZh());
        baseRole2.setName(baseRole.getName());
//        if(baseRoleService.baseRolesExit(baseRole1).size()>0){
//            return CommonStatus.ROLE_NAME_REPEAT;
//        }
        if(baseRoleService.baseRolesExit(baseRole2).size()>0){
            return CommonStatus.ROLE_NAME_REPEAT;
        }
        baseRole.setCreateBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        baseRole.setCreateTime(new Date());
        //加入所属网点ID
        baseRole.setSiteId(LoginDetailUtils.getSiteId());
        baseRoleService.addNewBaseRole(baseRole);
        System.out.println(baseRole.getId());
        return baseRole.getId();
    }

    @PutMapping(value = "baseRole")
    @ApiOperation(value = "修改角色", notes = "修改角色")
    public Object updateBaseRole(@Validated @RequestBody BaseRoleUpdateDto baseRoleUpdateDto) {
        BaseRole baseRole = new BaseRole();
        baseRole.setName(baseRoleUpdateDto.getName());
        baseRole.setNameZh(baseRoleUpdateDto.getName());
        baseRole.setRemark(baseRoleUpdateDto.getRemark());
        baseRole.setId(baseRoleUpdateDto.getId());
//        BaseRole baseRole1 = new BaseRole();
        BaseRole baseRole2 = new BaseRole();
//        baseRole1.setId(baseRole.getId());
//        baseRole1.setNameZh(baseRole.getNameZh());
        baseRole2.setId(baseRole.getId());
        baseRole2.setName(baseRole.getName());
//        if(baseRoleService.baseRolesExit(baseRole1).size()>0){
//            return CommonStatus.ROLE_NAME_REPEAT;
//        }
        if(baseRoleService.baseRolesExit(baseRole2).size()>0){
            return CommonStatus.ROLE_NAME_REPEAT;
        }
        baseRole.setModifyBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        baseRole.setModifyTime(new Date());
        baseRoleService.updateBaseRole(baseRole);
        return baseRole.getId();
    }

    @GetMapping(value = "baseRole/menu/{rid}")
    @ApiOperation(value = "获取菜单", notes = "根据角色Id获取角色对应菜单")
    public Map<String, Object> baseMenuTree(@PathVariable Integer rid) {
        Map<String, Object> map = new HashMap<>();
        List<BaseMenu> baseMenus = baseMenuService.baseMenuTree();
        map.put("baseMenus", baseMenus);
        List<Long> selMids = baseMenuService.getBaseMenusByRid(rid);
        map.put("mids", selMids);

        return map;
    }

    @PostMapping(value = "baseRole/roleMenu")
    @ApiOperation(value = "角色添加权限", notes = "给角色添加权限/菜单")
    public RestStatus updateBaseMenuRole(@RequestBody BaseRoleAddMenuDto baseRoleAddMenuDto) {
        BaseRole baseRole = new BaseRole();
        baseRole.setId(baseRoleAddMenuDto.getId());
        baseRole.setMids(baseRoleAddMenuDto.getMids());
        baseMenuBaseRoleService.updateBaseMenuRole(baseRole.getId().longValue(), baseRole.getMids());
        return CommonStatus.ADD_OK;
    }

    @GetMapping("baseRole")
    @ApiOperation(value = "查角色列表", notes = "查询所有角色列表")
    public Page<BaseRole> pageBaseRoles(@ModelAttribute BaseRole baseRole) {
//        if (LoginDetailUtils.getSiteId()==1){
//            baseRole.setRid("1");
//        }else {
//            baseRole.setRid("2");
//        }
//        baseRole.setSiteId(LoginDetailUtils.getSiteId());
        return baseRoleService.baseRoles(new Page<BaseRole>(baseRole.getPage(),baseRole.pageSize),baseRole);
    }


    @GetMapping("allBaseRole")
    @ApiOperation(value = "查询所有角色列表", notes = "查询所有角色列表")
    public List<BaseRole> allBaseRoles(@ModelAttribute BaseRole baseRole) {
//        if (LoginDetailUtils.getSiteId()==1){
//            baseRole.setRid("1");
//        }else {
//            baseRole.setRid("2");
//        }
//        baseRole.setSiteId(LoginDetailUtils.getSiteId());
        return baseRoleService.allBaseRoles(baseRole);
    }

    @GetMapping("baseRoleExport")
    @ApiOperation(value = "角色导出", notes = "角色导出")
    public void baseRolesExport(@ModelAttribute BaseRole baseRole, HttpServletResponse response) {
//        if (LoginDetailUtils.getSiteId()==1){
//            baseRole.setRid("1");
//        }else {
//            baseRole.setRid("2");
//        }
//        baseRole.setSiteId(LoginDetailUtils.getSiteId());
        try{
            ExcelUtil.exportExcel(baseRoleService.allBaseRoles(baseRole),"角色管理","角色管理",BaseRole.class,"角色管理"+UUIDUtil.getUUId()+".xls",response);
        }catch (RestException e){
            log.error("角色管理导出失败"+e.getMessage());
        }
    }

    @PostMapping(value = "/addDep")
    @ApiOperation(value = "新增部门", notes = "新增部门")
    public Map<String, Object> addDep(@RequestBody BaseDepartment baseDepartment) {
        Map<String, Object> map = new HashMap<>();
        if (baseDepartmentService.addDep(baseDepartment) >= 1) {
            map.put("status", "success");
            map.put("msg", baseDepartment);
            return map;
        }
        map.put("status", "error");
        map.put("msg", "添加失败!");
        return map;
    }

    @PostMapping(value = "/deleteDep")
    @ApiOperation(value = "删除部门", notes = "根据部门Id删除部门")
    public RestStatus deleteDep(@RequestBody Long did) {
        baseDepartmentService.deleteDep(did);
        return CommonStatus.DELETE_OK;
    }

    @PostMapping(value = "/getDepByPid")
    @ApiOperation(value = "查部门", notes = "根据部门Id查询")
    public List<BaseDepartment> getDepByPid(@RequestBody Long pid) {
        return baseDepartmentService.getDepByPid(pid);
    }

    @GetMapping(value = "/getAllDeps")
    @ApiOperation(value = "查部门列表", notes = "查部门列表")
    public List<BaseDepartment> getAllDeps() {
        return baseDepartmentService.getAllDeps();
    }


    @GetMapping(value = "/exportAllDeps")
    @ApiOperation(value = "导出部门列表", notes = "导出部门列表")
    public void exportAllDeps(HttpServletResponse response) {
        try{
            ExcelUtil.exportExcel(baseDepartmentService.getAllDeps(),"部门管理","部门管理",BaseDepartment.class,"部门管理"+UUIDUtil.getUUId()+".xls",response);
        }catch (RestException e){
            log.error("部门管理导出失败"+e.getMessage());
        }
    }

    @GetMapping(value = "/getPageDeps")
    @ApiOperation(value = "分页查部门列表", notes = "分页查部门列表")
    public Page<BaseDepartment> getPageDeps(@ModelAttribute BaseDepartmentqueryDto baseDepartmentqueryDto) {

        BaseDepartment baseDepartment = new BaseDepartment();
        baseDepartment.setName(baseDepartmentqueryDto.getName());
        baseDepartment.setPage(baseDepartmentqueryDto.getPage());
        baseDepartment.setPageSize(baseDepartmentqueryDto.getPageSize());

        return baseDepartmentService.getPageDeps(new Page<BaseDepartment>(baseDepartment.getPage(),baseDepartment.getPageSize()),baseDepartment);
    }


    @PutMapping(value = "/modifyDep")
    @ApiOperation(value = "修改部门", notes = "修改部门")
    public Map<String, Object> modifyDep(@RequestBody BaseDepartmentUpdateVO baseDepartment) {
        Map<String, Object> map = new HashMap<>();
        if (baseDepartmentService.modifyDep(baseDepartment) == 1) {
            map.put("status", "success");
            map.put("msg", baseDepartment);
            return map;
        }
        map.put("status", "error");
        map.put("msg", "添加失败!");
        return map;
    }

    @GetMapping("baseRole/code")
    @ApiOperation(value = "获取岗位编号", notes = "新增时获取岗位编号")
    public String getAutoCode(){
        return baseRoleService.getAutoCode();
    }

}
